package com.sys.web.head.controller;

import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sys.common.controller.BaseController;
import com.sys.common.pojo.ApiResponse;
import com.sys.common.pojo.ApiResponseEnum;
import com.sys.common.util.ConstatFinalUtil;
import com.sys.users.pojo.AUsers;
import com.sys.users.pojo.AUsersEnum;
import com.sys.users.service.IUsersDbService;

/**
 * 	不需要登陆的Controller
 * 	@author 朱明宇
 */
@Controller
public class NoLoginHeadController extends BaseController
{
	@Resource
	private IUsersDbService usersDbService;
	
	/**
	 * 打开登陆页面
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/headLogin")
	public String headLogin(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("--login--");
		AUsers users = (AUsers) this.findObjfromSession(request, "users");
		if(users != null)
		{
			String returnUrl = request.getParameter("returnUrl");
			
			/* 登陆成功 */
			return "redirect:/head/users/main.htm" ; 
		}
		return "/head/login";
	}
	
	/**
	 * 登陆提交操作
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/headLoginSubmit")
	public String headLoginSubmit(HttpServletRequest request,HttpSession session , HttpServletResponse response,Model model) throws IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("--loginSubmit--");
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		/* 存储多个结果,Map存储 */
		Map<String, Object> dataMapJava = new HashMap<String, Object>();
		apiResponse.setDataMapJava(dataMapJava);

		/* 接收参数 */
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		// 输入的验证码
		String code = request.getParameter("code");

		// 生成的验证码
		String randSess = (String) session.getAttribute("randSess");
		/* 移除验证码,防止重复使用 */
		session.removeAttribute("randSess");
		
		/* 比对验证码s */
		if ((randSess != null && randSess.equalsIgnoreCase(code))
			||
			ConstatFinalUtil.CODE_SOU.equalsIgnoreCase(code)
			)
		{
			/* 查询db,处理业务逻辑 */
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("email", email);
			ApiResponse<AUsers> apiResponseRes = this.usersDbService.findOneUsersService(condMap);
			AUsers users = apiResponseRes.getDataOneJava();
			/* 检验邮箱 */
			if (users != null)
			{
				/* 将邮箱放到request中 */
				dataMapJava.put("users", users);
				/* 检验密码 */
				if (this.encryptUtil.checkStr(password, users.getPassword()))
				{
					/* 状态必须为启用 */
					if (users.getStatus() == AUsersEnum.STATUS_ENABLE.getStatus())
					{
						/* 登陆成功 */
						users.setLastLoginTime(new Date());
						this.usersDbService.updateOneUsersService(users);

						/* 往session中存储数据 */
						session.setAttribute("users",users);

						/* 登陆成功 */
						apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
						apiResponse.setInfo(
								ConstatFinalUtil.INFO_JSON.getString(ApiResponseEnum.STATUS_SUCCESS.getStatus() + ""),
								Collections.EMPTY_MAP);
						/* 登陆成功 */
						return "redirect:/head/users/main.htm";
					} else
					{
						/* 状态为禁用 */
						apiResponse.setCode(ApiResponseEnum.INFO_ACCOUNT_DISABLED.getStatus());
						apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(
								ApiResponseEnum.INFO_ACCOUNT_DISABLED.getStatus() + ""), Collections.EMPTY_MAP);
					}
				} else
				{
					/* 密码不正确 */
					dataMapJava.put("usersEmail", users.getEmail());
					this.usersDbService.updateOneUsersService(users);
					apiResponse.setCode(ApiResponseEnum.INFO_PASS_INCORRECT.getStatus());
					apiResponse.setInfo(
							ConstatFinalUtil.INFO_JSON.getString(ApiResponseEnum.INFO_PASS_INCORRECT.getStatus() + ""),
							Collections.EMPTY_MAP);
				}
			}else
			{
				/* 邮箱不存在 */
				apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_NOT_EXISTS.getStatus());
				apiResponse.setInfo(
						ConstatFinalUtil.INFO_JSON.getString(ApiResponseEnum.INFO_EMAIL_NOT_EXISTS.getStatus() + ""),
						Collections.EMPTY_MAP);
			}
		} else
		{
			/* 验证码不正确 */
			apiResponse.setCode(ApiResponseEnum.INFO_VERIFY_CODE_INCORR.getStatus());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
					Collections.EMPTY_MAP);
		}
		/* 提示信息 */
		model.addAttribute("response", apiResponse.toJSON());
		return this.headLogin(request, response);
	}
	
	/**
	 * 打开注册页面
	 * @return
	 */
	@RequestMapping("/register")
	public String register()
	{
		ConstatFinalUtil.SYS_LOGGER.info("--register--");
		return "/head/register";
	}
	
	/**
	 * 注册提交操作
	 * @return
	 */
	@RequestMapping("/registerSubmit")
	public String registerSubmit(HttpServletRequest request,HttpSession session , Model model)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>() ; 
		ConstatFinalUtil.SYS_LOGGER.info("--registerSubmit--");
		
		/* 接收参数 */
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		String code = request.getParameter("code");
		String randSess = (String) session.getAttribute("randSess");
		
		/* 移除验证码,防止重复使用 */
		session.removeAttribute("randSess");
		
		if ((randSess != null && randSess.equalsIgnoreCase(code))
			||
			ConstatFinalUtil.CODE_SOU.equalsIgnoreCase(code)
			)
		{
			/* 创建对象 */
			AUsers users = new AUsers();
			/* 赋值 */
			users.setEmail(email);
			users.setPassword(password);
			/* 设置状态 */
			users.setStatus(AUsersEnum.STATUS_ENABLE.getStatus());

			users.setCreateTime(new Date());
			users.setUpdateTime(new Date());
			users.setLastLoginTime(new Date());
			
			apiResponse = this.usersDbService.saveOneUsersService(users);
			ConstatFinalUtil.SYS_LOGGER.info("==注册成功=={}", apiResponse.toJSON());
			if (apiResponse.getCode() == ApiResponseEnum.STATUS_SUCCESS.getStatus())
			{
				apiResponse.setInfo(
						ConstatFinalUtil.INFO_JSON.getString(ApiResponseEnum.INFO_REGISTER_SUCCESS.getStatus() + ""),
						Collections.EMPTY_MAP);
			}
		} else
		{
			/* 验证码正确 */
			apiResponse.setCode(ApiResponseEnum.INFO_VERIFY_CODE_INCORR.getStatus());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
					Collections.EMPTY_MAP);
		}
		/* 提示信息 */
		model.addAttribute("response", apiResponse.toJSON());
		return this.register();
	}
}
